/**
 * Editor of ayw: for comment, for daily
 * root selector must be a form
 * 
 * @zli <thinkerli_2009@hotmail.com>
 * $Id $
 */
$(function () {
    $.widget('ui.aywSimpleEditor', {
		options: {
			selectors: {editor:'[name="content"]', save:'[type="submit"]'}, 
			functions: ["emotion", "topic", "pic", "video", "music"],//more
			word_limit: 140,
			ajaxSubmit: true,
			success: function(){}
		},
		_editor : null,
		_publishHandler: null,
		
		_create: function() {
			var self = this;
			self._editor = $(self.options.selectors.editor, self.element);
			self._publishHandler = $(self.options.selectors.save, self.element);
			if (self._editor.length == 0)  return false;
			
			
			self._publishHandler.on('click.'+ self.widgetName, function() {
				if (self.options.ajaxSubmit) {
					return self._initSubmit();
				} else {
					return true;
				}
			});
			
			// init functions
			if ($.isArray(self.options.functions)) {
				for (var i=0; i<self.options.functions.length; i++) {
					var _name = self.options.functions[i], _selector = '.icon_small_' + _name, _func = 'self._' + _name + '(this);' ;
					$(_selector).parent('a').data('func', _func).on('click.' + self.widgetName, function(){
						eval($(this).data('func'));
						//ui.emotions(this,self._editor.attr('id'));
						return false;
					});
				}
			}
		},
		
		_initSubmit: function() {
			var self = this;
			var options = {
					    success: function(txt) {
					    	self.options.success.call(self, txt);
					    }
					};
			if (self._editor.val().length > 1 && self._editor.val().length < self.options.word_limit ) {
				$(self.element).ajaxSubmit( options );
				return false;
			} else {
				ui.error('字符数在1-' + self.options.word_limit + '之间');
				self._editor.focus();
				return false;
			}
		},
		
		_emotion: function(handler) {
			ui.emotions(handler, this._editor.attr('id'));
			return false;
		},
		
		_topic: function() {
			var self = this;
			var text = '#请在这里输入自定义话题#';
			var   patt   =   new   RegExp(text,"g");  
			var content_publish = self._editor;
			var result;
						
			if( content_publish.val().search(patt) == '-1' ){
				content_publish.val( content_publish.val() + text);
			}
			
			var textArea = self._editor;
			
			result = patt.exec( content_publish.val() );
			
			var end = patt.lastIndex-1 ;
			var start = patt.lastIndex - text.length +1;
			
			if (document.selection) { //IE
				 var rng = textArea.createTextRange();
				 rng.collapse(true);
				 rng.moveEnd("character",end)
				 rng.moveStart("character",start)
				 rng.select();
			}else if (textArea.selectionStart || (textArea.selectionStart == '0')) { // Mozilla/Netscape…
		        textArea.selectionStart = start;
		        textArea.selectionEnd = end;
		    }
		    textArea.focus();
			//weibo.checkInputLength('#content_publish', _LENGTH_);
			return ;
		},
		
		//检查字数输入
		_checkInputLength: function(obj,num){
			var self = this;
			var len = getLength($(obj).val(), true);
			var wordNumObj = $('.wordNum');
			
			if(len==0){
				wordNumObj.css('color','').html('你还可以输入<strong id="strconunt">'+ (num-len) + '</strong>字');
				self.textareaStatus('off');
			}else if( len > num ){
				wordNumObj.css('color','red').html('已超出<strong id="strconunt">'+ (len-num) +'</strong>字');
				self.textareaStatus('off');
			}else if( len <= num ){
				wordNumObj.css('color','').html('你还可以输入<strong id="strconunt">'+ (num-len) + '</strong>字');
				self.textareaStatus('on');
			}
		},
		
		//发布按钮状态
		_textareaStatus: function(type){
			var obj = self._publishHandler;
			if(type=='on'){
				obj.removeAttr('disabled').attr('class','btn_big_disable hand');
			}else{
				obj.attr('disabled','true').attr('class','btn_big_disable hand');
			}
		}	
	});
});

